//
//  BDLoadingView.m
//  爱贝儿医
//
//  Created by 王磊 on 6/13/16.
//  Copyright © 2016 爱贝儿医. All rights reserved.
//

#import "BDLoadingView.h"
@interface BDLoadingView()

@property (nonatomic ,strong) UIImageView *iconImageView;

@end
static BDLoadingView *shareInstance = nil;

@implementation BDLoadingView

- (UIButton *)backButton {
    if (!_backButton) {
        _backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [_backButton setImage:[UIImage imageNamed:@"灰色返回"] forState:UIControlStateNormal];
        [_backButton setImage:[UIImage imageNamed:@"灰色返回"] forState:UIControlStateHighlighted];
        
        [_backButton addTarget:self action:@selector(backButtonTriggered) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:_backButton];
        
        [_backButton makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(0);
            make.top.equalTo(20);
            make.width.equalTo(60);
            make.height.equalTo(44);
        }];
    }
    return _backButton;
}

- (UILabel *)reloadLabel {
    if (!_reloadLabel) {
        _reloadLabel = [UILabel new];
        _reloadLabel.backgroundColor = [UIColor clearColor];
        
        _reloadLabel.font = [UIFont systemFontOfSize:15];
        _reloadLabel.textColor = STRGBColor(204,204 ,204);
        _reloadLabel.textAlignment = NSTextAlignmentCenter;
        _reloadLabel.text = @"点击屏幕 重新加载";
        
        [self addSubview:_reloadLabel];
        [_reloadLabel makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(ST_SCREEN_WIDTH);
            make.centerX.equalTo(0);
            make.centerY.equalTo(50 - ST_SCREEN_WIDTH / 16);
            make.height.equalTo(ST_SCREEN_WIDTH / 8);
        }];
    }
    return _reloadLabel;
}
- (UIActivityIndicatorView *)activity {
    if (!_activity) {
        _activity = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
        [self addSubview:_activity];
        
        [_activity makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(50);
            make.height.equalTo(50);
            make.centerX.equalTo(0);
            make.centerY.equalTo(0);
        }];
    }
    return _activity;
}
- (UIImageView *)iconImageView {
    if (!_iconImageView) {
        _iconImageView = [[UIImageView alloc]init];
        //        _iconImageView.image = [UIImage imageNamed:HZS_BACKGROUND_IMAGE];
        _iconImageView.contentMode = UIViewContentModeScaleAspectFit;
        
        [self addSubview:_iconImageView];
        
        _iconImageView.frame = self.bounds;
        
    }
    return _iconImageView;
}
+ (BDLoadingView *)loadingView:(CGRect)frame {
    return [[self alloc]initWithFrame:frame];
}
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = ST_BG_COLOR;
        
        [self tapGR];
    }
    return self;
}
- (void)tapGR {
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(reloadTriggered)];
    
    tap.numberOfTapsRequired = 1;
    
    [self addGestureRecognizer:tap];
}
+ (BDLoadingView *)loadingView {
    return [[self alloc]init];
}
- (instancetype)init {
    if (self = [super init]) {
        
        self.frame = ST_SCREEN_BOUNDS;
        
        self.backgroundColor = ST_BG_COLOR;
        
        [self tapGR];
    }
    return self;
}

- (void)show {
    
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    
    [keyWindow addSubview:self];
    
    [self.activity startAnimating];
    
    self.iconImageView.hidden = false;
    
    self.reloadLabel.hidden = true;
    
    [self bringSubviewToFront:self.backButton];
    
    [self bringSubviewToFront:self.activity];
    
}
- (void)setLoadingStatus:(BDLoadingStatus)loadingStatus {
    _loadingStatus = loadingStatus;
    switch (loadingStatus) {
        case BDLoadingStatusLoading:
        {
            [self show];
        }
            break;
        case BDLoadingStatusSuccess:
        {
            [self dismiss];
        }
            break;
        case BDLoadingStatusFailure:
        {
            [self onFailure];
        }
            break;
            
        default:
        {
            [self onFailure];
        }
            break;
    }
}
- (void)onReload {
    [self.activity startAnimating];
    
    self.reloadLabel.hidden = true;
}
- (void)onFailure {
    
    self.reloadLabel.hidden = false;
    
    [self.activity stopAnimating];
}
- (void)reloadTriggered {
    
    [self onReload];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(loadingView: onListenLoadingStatus:)]) {
        
        [self.delegate loadingView:self onListenLoadingStatus:self.loadingStatus];
        
    }
}
- (void)dismiss {
    
    if (self.activity.isAnimating) {
        [self.activity stopAnimating];
    }
    __weak typeof(self) weakSelf = self;
    
    [UIView animateWithDuration:0.2 animations:^{
        
        weakSelf.alpha = 0.0;
        
    } completion:^(BOOL finished) {
        
        [weakSelf removeFromSuperview];
        
    }];
}
- (void)backButtonTriggered {
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(onBackItemClick)]) {
        [self.delegate onBackItemClick];
    }
}
@end

